package com.nulldev.util.uri.server.v2;

import com.nulldev.util.other.ArrayUtils;

public enum HttpCompressionMethod {
	NONE(null),
	
	/**
	 * Successor to IWEB_ENC. <br>
	 * Encryption Method: RSA 2048 also supports RSA 4096 <br> 
	 * Required Headers: <br>
	 * - iWebKit.publicKey = Public Key of RSA, encoded in Base 64 <br>
	 * - iWebKit.Secure.Method = Method Used [Allowed: RSA_2048, RSA_4096] <br>
	 * - Content-Encoding-Type: [iWebKitSecure.RSA_2048, iWebKitSecure.RSA_4096] <br>
	 * Method: <br>
	 * Client Connect -> Send Required Headers -> Server Processes -> Server Encrypts -> Replies -> END <br>
	 * Transfer Method: 8KB chunked encryption <br>
	 * TODO: Impelement reverse method for clients sending to serverz <br>
	 * Probable method:
	 * Client Connect -> Visit /iwbk_pk_key to get base 64 encoded public key -> continue.
	 */
	iWebKitSecure("iWebKitSecure"),
	
	GZIP("gzip"),
	DEFLATE("deflate"),
	/**
	 * iWebKit: Encrypted [OLD, new method name: iWebKitSecure] <br>
	 * XXTEA encrypted response <br>
	 * MARKED_FOR_REMOVAL_REASON: Doesn't work well with large data, uses XXTEA, an insecure cipher <br>
	 * 1.0 - Full data encryption <br>
	 * (CURRENT) 1.1 - Data is encrypted in 4KB chunks.
	 */
	@Deprecated
	IWEB_ENC("iweb_enc"),
	;
	
	private final String val;
	private HttpCompressionMethod(final String value) {
		this.val = value;
	}
	
	public boolean isValue() {
		return this.val != null;
	}
	
	public String value() {
		return this.val;
	}
	
	public static HttpCompressionMethod parse(final String value) {
		if (value == null || value.length() == 0) {
			return NONE;
		}
		final String[] values = value.split(", ");
		for (final HttpCompressionMethod method : values()) {
			if (!method.isValue()) {
				continue;
			}
			if (ArrayUtils.StringArrayUtils.contains(values, method.value())) {
				return method;
			}
		}
		return NONE;
	}
}
